package com.member.common.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @date 2019/1/17
 */
public class OperationFileUtil {

    public void downFile(String fileName, HttpServletRequest request, HttpServletResponse response) {

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            if(!"".equals(fileName)){
                String suffix = fileName.substring(fileName.indexOf(".")+1,fileName.length());
                if(fileName.contains("..") || fileName.contains("/")){
                    System.out.println("非法操作");
                    return;
                }else{
                    if("".equals(suffix) ||!"zip".equals(suffix)&&!"docx".equals(suffix)&&!"pptx".equals(suffix)&&!"xlsx".equals(suffix)&&!"exe".equals(suffix)){
                        System.out.println("文件类型有误");
                        return;
                    }
                }

            }
        }catch (Exception e) {
            System.out.println("下载文件异常，异常原因: ");
            e.printStackTrace();
        } finally {
            //关闭流
            if(bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            request.setCharacterEncoding("UTF-8");

            //获取项目根目录
            String ctxPath = request.getSession().getServletContext().getRealPath("");

            //获取下载文件路径
            String downLoadPath = ctxPath+"/lodop/"+ fileName;

            File file = new File(downLoadPath);
            if(!file.exists()){
                System.out.println("下载文件异常，异常原因: [" + downLoadPath + "]文件不存在");
                return;
            }

            //获取文件的长度
            long fileLength = new File(downLoadPath).length();

            //设置文件输出类型
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
            //设置输出长度
            response.setHeader("Content-Length", String.valueOf(fileLength));
            //获取输入流
            bis = new BufferedInputStream(new FileInputStream(downLoadPath));
            //输出流
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            System.out.println("下载文件异常，异常原因: ");
            e.printStackTrace();
        } finally {
            //关闭流
            if(bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
